// 计算商品总价
function calculateTotal(products) {
    let total = 0;
    products.forEach(item => {
        total += item.price * item.quantity;
    });
    return total;
}

// 示例数据
const cart = [
    { name: '苹果', price: 5, quantity: 2 },
    { name: '香蕉', price: 3, quantity: 4 }
];

const total = calculateTotal(cart);
console.log(`总价：${total}元`); // 输出：总价：22元